using Autofac;
using Jinjia.Core;
using Jinjia.Core.ScheduleProvider;
using Jinjia.Approve.ScheduleJobAPI.Config;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Jinjia.Core.HttpClient;
using Jinjia.Core.SSO;
using Jinjia.Core.Middleware;
using Jinija.Core.Common;
using Jinjia.Approve.Model.Config;
using JinJia.Core.Cache;
using Jinjia.Core.TransactionServiceExtensions;

namespace Jinjia.Approve.ScheduleJobAPI
{
    public class Startup
    {
        private IConfiguration Configuration { get; }
        private IEnumerable<Assembly> Assemblies { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "Jinjia.Approve.*.dll")
                .Select(Assembly.LoadFrom).ToArray();
        }

        public void ConfigureContainer(ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterType<SwaggerGenerator>().AsSelf().InstancePerLifetimeScope();
            containerBuilder.RegisterModules(Assemblies);
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddCacheService(Configuration.GetSection("JinjiaCache"));
            services.AddHealthChecks();
            services.AddConfigOptions(Configuration);
            services.AddAutoMapper(Assemblies);
            services.RegisterHttpClient();
            services.AddHttpContextAccessor();
            services.AddHttpClientTraceService();
            services.AddBaseService(Configuration);
            services.AddHttpClientDelegatingHandlerService(Configuration);
            services.UseDefaultAuthentication(Configuration.GetSection("SSOConfig"));
            services.Configure<UrlConfigOption>(Configuration.GetSection("UrlConfig"));
            services.GenerateSwaggerDoc();
            services.AddScheduleJob();
            services.AddCapService(Configuration.GetSection("DistributedTransaction"), capOption =>
            {
                capOption.FailedRetryCount = 15;
                capOption.FailedRetryInterval = 150;
                capOption.UseDashboard(dashboardOption =>
                {
                    dashboardOption.PathMatch = "/approve/cap";
                });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwaggerMiddleware();
            }
            else
            {
                app.UseExceptionMiddleware();
            }

            app.UseEnableBufferMiddleware();
            //app.ApplicationServices.GetService<JinJia.Core.MQProvider.Consumer.IRabbitMQConsume>().StartConsumer();//����������
            app.UseHttpsRedirection();
            app.UseJobRequestMiddleware();
            app.UseRouting();
            app.UseDefaultAuthorization();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health");
            });
        }
    }
}